package com.seasun.xg.lamp.web;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import com.seasun.xg.lamp.repo.LogVo;

public interface WebRepository extends CrudRepository<LogVo, Integer> {

	@Query(value = ""
			+ "select"
			+ "    *"
			+ "  from log_content"
			+ "  where seq in ("
			+ "    select"
			+ "        seq"
			+ "      from log_seq"
			+ "      order by id desc"
			+ "      fetch next ?1 rows only)"
			+ "  order by seq desc, db_timestamp", nativeQuery = true)
	List<LogVo> getLatestSeq(int count);

	@Query(value = ""
			+ "select"
			+ "    *"
			+ "  from log_content c1"
			+ "  where exists ("
			+ "	select"
			+ "	    seq"
			+ "	  from log_content c2"
			+ "	  where seq in ("
			+ "	    select"
			+ "	        seq"
			+ "	      from log_seq"
			+ "	      order by id desc"
			+ "	      fetch next ?1 rows only)"
			+ "	    and (?2 = '' or lower(device_id) like lower(?2))"
			+ "	    and (?3 = '' or lower(local_ip) like lower(?3))"
			+ "	    and (?4 = '' or lower(public_ip) like lower(?4))"
			+ "	    and (?5 = '' or lower(level) like lower(?5))"
			+ "	    and (?6 = '' or lower(app_name) like lower(?6))"
			+ "	    and (?7 = '' or lower(app_version) like lower(?7))"
			+ "	    and (?8 = '' or lower(content) like lower(?8))"
			+ "	    and c1.seq = c2.seq)"
			+ "  order by seq desc, db_timestamp", nativeQuery = true)
	List<LogVo> search(int count, String deviceId, String localIp, String publicIp, String level, String appName,
			String appVersion, String content);

	List<LogVo> findBySeqOrderByDbTimestampAsc(String seq);
}
